package com.zto.sch.base.util;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * @program: sch-diy-parent
 * @description:
 * @author: sch
 * @create: 2021-07-09 01:09
 **/
public class CommonUtil {
    public static int[] getArray(int i) {
        int[] arr = new int[i];
        for (int j = 0; j < i; j++) {
            arr[j] = j;
        }

        return arr;
    }

    public static Integer[] getRandomArray(int i) {
        Integer[] arr = new Integer[i];
        Random random = new Random();
        for (int j = 0; j < i; j++) {
            if (j > 0) {
                List<Integer> list = Arrays.asList(arr);

                int temp = random.nextInt(i);
                if (list.contains(temp)) {
                    while (true) {
                        temp = random.nextInt(i);
                        if (!list.contains(temp)) {
                            break;
                        }
                    }
                }
                arr[j] = temp;
            } else {
                arr[j] = random.nextInt(i);
            }
        }

        return arr;
    }

    /**
     * 随机指定范围内N个不重复的数
     * 最简单最基本的方法
     *
     * @param min 指定范围最小值
     * @param max 指定范围最大值
     * @param n   随机数个数
     */
    public static int[] randomCommonArr(int min, int max, int n) {
        if (n > (max - min + 1) || max < min) {
            return null;
        }
        int[] result = new int[n];
        int count = 0;
        while (count < n) {
            int num = (int) (Math.random() * (max - min)) + min;
            boolean flag = true;
            for (int j = 0; j < n; j++) {
                if (num == result[j]) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                result[count] = num;
                count++;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            System.out.println(Math.random());

        }
    }
}
